package com.tianjian.myblog.service;

import com.tianjian.myblog.dao.TypeRepository;
import com.tianjian.myblog.exception.NotFoundException;
import com.tianjian.myblog.model.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeRepository repository;

    @Transactional
    @Override
    public Type saveType(Type type) {
        return repository.save(type);
    }
    @Transactional
    @Override
    public Type updateType(Long id, Type type) {

        Type t = repository.findOne(id);
        if (t==null){
            throw new NotFoundException("分类不存在");
        }
        BeanUtils.copyProperties(type,t);

        return repository.save(t);
    }
    @Transactional
    @Override
    public void deleteType(Long id) {

        repository.delete(id);

    }

    @Transactional
    @Override
    public Type findOneType(Long id) {
        return repository.findOne(id);
    }
    @Transactional
    @Override
    public Type findByName(String name) {
        return repository.findByName(name);
    }

    @Transactional
    @Override
    public Page<Type> findType(Pageable pageable) {
        return repository.findAll(pageable);
    }
}
